package com.example.poi_demo.controller;
import com.google.common.collect.Lists;
import java.util.Date;
import com.google.common.collect.Maps;
import com.example.poi_demo.entity.SysDept;

import com.example.poi_demo.entity.SysUser;
import com.example.poi_demo.utils.ExcelUtil;
import lombok.extern.java.Log;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TestController {

    // 导入数据
    @PostMapping("/importData")
    public Map importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
        List<SysUser> userList = util.importExcel(file.getInputStream());
        HashMap<String, Object> result = new HashMap<>();
        result.put("data",userList);
        return result;
    }

    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
        util.importTemplateExcel(response, "用户数据");
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response, SysUser user)
    {
        List<SysUser> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            SysUser sysUser = new SysUser();
            sysUser.setUserId((long)i);
            sysUser.setDeptId((long)i);
            sysUser.setNickName("AAAA"+i);
            sysUser.setUserName("BBBB"+i);
            sysUser.setEmail("AAAA@qq.com");
            sysUser.setPhonenumber("111111");
            sysUser.setSex("1");
            sysUser.setAvatar("111");
            sysUser.setPassword("111");
            sysUser.setStatus("1");
            sysUser.setDelFlag("1");
            sysUser.setLoginIp("192.168.0.1");
            sysUser.setLoginDate(new Date());
            sysUser.setDept(new SysDept());
            sysUser.setRoles(Lists.newArrayList());
//            sysUser.setRoleIds(new Long[]());
//            sysUser.setPostIds(new Long[]());
            sysUser.setRoleId(0L);
            sysUser.setSearchValue("aaaa");
            sysUser.setCreateBy("aaaa");
            sysUser.setCreateTime(new Date());
            sysUser.setUpdateBy("aaaa");
            sysUser.setUpdateTime(new Date());
            sysUser.setRemark("aaaaaaa");
            list.add(sysUser);
        }
        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
        util.exportExcel(response, list, "用户数据");
    }
}
